package com.ruibang.glass.produce.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruibang.glass.produce.domain.req.JobReportReq;
import com.ruibang.glass.produce.domain.resp.JobReportResp;
import com.ruibang.glass.produce.domain.resp.MainIngredientInfo;
import com.ruibang.glass.produce.domain.resp.MainIngredientWeighingInfo;
import com.ruibang.glass.produce.domain.resp.SmallMaterialInfo;
import com.ruibang.glass.produce.entity.MainIngredientsRecord;
import com.ruibang.glass.produce.entity.MaterialFeedRecord;
import com.ruibang.glass.produce.entity.MaterialPremixRecord;
import com.ruibang.glass.produce.service.*;
import com.ruibang.glass.produce.util.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: songJinKang
 * @CreateTime: 2024-05-07  11:02
 * @Description: TODO
 * @Version: 1.0
 */

@Service
public class JobReportServiceImpl implements JobReportService {

    @Autowired
    private ClassReportService classReportService;

    @Autowired
    private MainIngredientsRecordService mainIngredientsRecordService;

    @Autowired
    private MaterialPremixRecordService materialPremixRecordService;

    @Autowired
    private MaterialFeedRecordService materialFeedRecordService;

    @Override
    public JobReportResp getReportInfo(JobReportReq jobReportReq) {
        if (jobReportReq != null && jobReportReq.getDate() != null) {
            LocalDate date = jobReportReq.getDate();

            JobReportResp jobReportResp = new JobReportResp();
            //获取主配料报表信息
            MainIngredientInfo mainIngredientInfo = classReportService.getMainIngredientInfo(date);
            jobReportResp.setMainIngredientInfo(mainIngredientInfo);

            //查询主配料称重误差趋势图
            List<MainIngredientsRecord> ingredientsRecordList = mainIngredientsRecordService.getIngredientInfoByDate(date);
            if (CollectionUtils.isNotEmpty(ingredientsRecordList)) {
                MainIngredientWeighingInfo mainIngredientWeighingInfo = processIngredientData(ingredientsRecordList);
                jobReportResp.setMainIngredientWeighingInfo(mainIngredientWeighingInfo);
            }

            //上料报表
            List<MaterialFeedRecord> materialFeedRecordList = materialFeedRecordService.getByDate(date);
            jobReportResp.setMaterialFeedRecord(materialFeedRecordList);

            //小料配料报表
            List<MaterialPremixRecord> materialPremixRecords = materialPremixRecordService.getByDate(date);
            if (CollectionUtils.isNotEmpty(materialPremixRecords)) {
                SmallMaterialInfo smallMaterialInfo = processMaterialPremixData(materialPremixRecords);
                if (mainIngredientInfo != null) {
                    smallMaterialInfo.setXl5(mainIngredientInfo.getXl5());
                    smallMaterialInfo.setXl6(mainIngredientInfo.getXl6());
                    smallMaterialInfo.setXl7(mainIngredientInfo.getXl7());
                }
                jobReportResp.setSmallMaterialInfo(smallMaterialInfo);
            }
            return jobReportResp;
        }
        return null;
    }


    /**
     * @Description: 处理小料数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-07 15:06
     */
    private SmallMaterialInfo processMaterialPremixData(List<MaterialPremixRecord> materialPremixRecords) {
        SmallMaterialInfo smallMaterialInfo = new SmallMaterialInfo();

        //批数
        smallMaterialInfo.setBatchNum(materialPremixRecords.size());

        //硅砂
        double silicaSand = materialPremixRecords.stream().mapToDouble(MaterialPremixRecord::getRealityMaterial1).sum();
        smallMaterialInfo.setSilicaSand(DataUtil.formatDouble(silicaSand));

        //合计
        double total = materialPremixRecords.stream().mapToDouble(MaterialPremixRecord::getRealityTotalWeight).sum();
        smallMaterialInfo.setTotal(DataUtil.formatDouble(total));

        //入I仓量/kg
        double putI = materialPremixRecords.stream().filter(e -> "I".equals(e.getSilo())).mapToDouble(MaterialPremixRecord::getRealityTotalWeight).sum();
        smallMaterialInfo.setPutI(DataUtil.formatDouble(putI));

        //入H仓量/kg
        double putH = materialPremixRecords.stream().filter(e -> "H".equals(e.getSilo())).mapToDouble(MaterialPremixRecord::getRealityTotalWeight).sum();
        smallMaterialInfo.setPutH(DataUtil.formatDouble(putH));

        //料方号
        List<String> materialCode = materialPremixRecords.stream().map(MaterialPremixRecord::getMaterialCode).distinct().collect(Collectors.toList());
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < materialCode.size(); i++) {
            if (i == materialCode.size() - 1) {
                stringBuilder.append(materialCode.get(i));
            } else {
                stringBuilder.append(materialCode.get(i)).append("、");
            }
        }
        smallMaterialInfo.setMaterialNumber(stringBuilder.toString());
        return smallMaterialInfo;
    }


    /**
     * @Description: 处理主配料误差称重趋势图数据
     * @version v1.0
     * @author songJinKang
     * @date 2024-05-07 15:06
     */
    private MainIngredientWeighingInfo processIngredientData(List<MainIngredientsRecord> ingredientsRecordList) {
        MainIngredientWeighingInfo mainIngredientWeighingInfo = new MainIngredientWeighingInfo();
        List<Integer> x = ingredientsRecordList.stream().map(MainIngredientsRecord::getJobNumber).collect(Collectors.toList());
        mainIngredientWeighingInfo.setX(x);

        List<Double> a = ingredientsRecordList.stream().map(MainIngredientsRecord::getAErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setAErrorWeight(a);

        List<Double> b = ingredientsRecordList.stream().map(MainIngredientsRecord::getBErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setBErrorWeight(b);

        List<Double> c = ingredientsRecordList.stream().map(MainIngredientsRecord::getCErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setCErrorWeight(c);

        List<Double> d = ingredientsRecordList.stream().map(MainIngredientsRecord::getDErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setDErrorWeight(d);

        List<Double> e = ingredientsRecordList.stream().map(MainIngredientsRecord::getEErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setEErrorWeight(e);

        List<Double> f = ingredientsRecordList.stream().map(MainIngredientsRecord::getFErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setFErrorWeight(f);

        List<Double> g = ingredientsRecordList.stream().map(MainIngredientsRecord::getGErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setGErrorWeight(g);

        List<Double> h = ingredientsRecordList.stream().map(MainIngredientsRecord::getHErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setHErrorWeight(h);

        List<Double> j = ingredientsRecordList.stream().map(MainIngredientsRecord::getJErrorWeight).collect(Collectors.toList());
        mainIngredientWeighingInfo.setJErrorWeight(j);

        return mainIngredientWeighingInfo;
    }
}











